package com.tmpt.others.jpush;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;

import java.util.ArrayList;
import java.util.List;


public class PushtoSingle {
    /*
        应用名称： 勤务系统
        appId： UyAVkvzOCf8BCIQifV3i26
        appSecret： cArXJOy3ld6aW1lk9fn0JA
        appKey： cWmzNoUahh6wILrLKHvJd9
        应用包名： com.chengguan.qinwu
    */

    //采用"Java SDK 快速入门"， "第二步 获取访问凭证 "中获得的应用配置，用户可以自行替换
//    private static String appId = "cqPsyBVK8V9jvu3u0Ljwv3";
//    private static String appKey = "OJpkW3zYmf9rQxDIYEt3A6";
//    private static String masterSecret = "9fuIvdFReZ6JQEp6tA2M33";
    private static String appId = "UyAVkvzOCf8BCIQifV3i26";
    private static String appKey = "cWmzNoUahh6wILrLKHvJd9";
    private static String masterSecret = "rbGemouBrtAwlPFzPIO2b4";

    //static String CID = "ead48e5718017e5943c87bd24819dd2c";
    //别名推送方式
    // static String Alias = "";
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";
    static IGtPush push = null;

    static{
        push = new IGtPush(host, appKey, masterSecret);
    }
    public static void push(List<String> clientIDList, String ALERT){
        //系统测试，关掉了，一会开。2019-02-02
        pushTransmissionTemplate(clientIDList,ALERT);
        pushNotificationTemplate(clientIDList,ALERT);
    }

    public static void pushTransmissionTemplate(List<String> clientIDList, String ALERT){
        try {
            ListMessage message = new ListMessage();
            TransmissionTemplate template = new TransmissionTemplate();
            template.setAppId(appId);
            template.setAppkey(appKey);
            template.setTransmissionContent(ALERT);
            template.setTransmissionType(1); // 这个Type为int型，填写1则自动启动app

            message.setData(template);
            message.setOffline(true);
            message.setOfflineExpireTime(1 * 1000);

            // 设置推送目标，填入appid和clientId
            List targets = new ArrayList<>();
            Target target = null;
            for(String clientID : clientIDList) {
                target = new Target();
                target.setAppId(appId);
                target.setClientId(clientID);
                targets.add(target);
            }

            try{
                String taskId = push.getContentId(message);
                push.pushMessageToList(taskId, targets);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.getLocalizedMessage();
        }
    }
    public static void pushNotificationTemplate(List<String> clientIDList, String ALERT){
        try {
            NotificationTemplate template = new NotificationTemplate();
            // 设置APPID与APPKEY
            template.setAppId(appId);
            template.setAppkey(appKey);
            Style0 style = new Style0();
            // 设置通知栏标题与内容
            style.setTitle("通知消息");
            style.setText(ALERT);
            // 配置通知栏图标
            style.setLogo("icon.png");
            // 配置通知栏网络图标
            style.setLogoUrl("");
            // 设置通知是否响铃，震动，或者可清除
            style.setRing(true);
            style.setVibrate(true);
            style.setClearable(true);
            template.setStyle(style);
            // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
            template.setTransmissionType(1);
            template.setTransmissionContent("请输入您要透传的内容");

            ListMessage message = new ListMessage();
            message.setOffline(true);
            // 离线有效时间，单位为毫秒，可选
            message.setOfflineExpireTime(1800 * 1000);
            message.setData(template);
            // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
            message.setPushNetWorkType(0);

            List targets = new ArrayList<>();
            Target target = null;
            for(String clientID : clientIDList) {
                target = new Target();
                target.setAppId(appId);
                target.setClientId(clientID);
                targets.add(target);
            }

            IPushResult ret = null;
            String taskId = push.getContentId(message);
            try {
                ret = push.pushMessageToList(taskId, targets);
            } catch (RequestException e) {
                e.printStackTrace();
                ret = push.pushMessageToList(taskId, targets);
            }
            if (ret != null) {
                //.out.println(ret.getResponse().toString());
            } else {
                //.out.println("服务器响应异常");
            }
        }catch (Exception e){
            e.getLocalizedMessage();
        }
    }

    public static void main(String[] args) throws Exception {
        //String CID = "831f7626c9f6ff9807521098a405a3a0";
        String CID = "5549df9af8fa29a67c7bb6c31d6be8eb";
        String ALERT = "测试111";
        List<String> clientIDList = new ArrayList<>();
        clientIDList.add(CID);
        PushtoSingle.push(clientIDList,ALERT);
        /*IGtPush push = new IGtPush(host, appKey, masterSecret);
        NotificationTemplate template = notificationTemplateDemo();
        ListMessage message = new ListMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);

        List targets = new ArrayList<>();
        Target target = null;
        target = new Target();
        target.setAppId(appId);
        target.setClientId(CID);
        targets.add(target);
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            //ret = push.pushMessageToSingle(message, target);
            String taskId = push.getContentId(message);
            ret = push.pushMessageToList(taskId, targets);
        } catch (RequestException e) {
            e.printStackTrace();
            //ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            //.out.println(ret.getResponse().toString());
        } else {
            //.out.println("服务器响应异常");
        }*/
    }

    public static NotificationTemplate notificationTemplateDemo(String ALERT) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("通知消息");
        style.setText(ALERT);
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent("请输入您要透传的内容");
        return template;
    }


    public static LinkTemplate linkTemplateDemo(String ALERT) {
        LinkTemplate template = new LinkTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("通知消息");
        style.setText(ALERT);
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        // 设置打开的网址地址
        template.setUrl("111");
        return template;
    }
    public static TransmissionTemplate getTemplate(String ALERT) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(ALERT);
        template.setTransmissionType(2);
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("1");
        payload.setContentAvailable(1);
        payload.setSound("default");
        payload.setCategory(ALERT);

        //简单模式APNPayload.SimpleMsg
//        payload.setAlertMsg(new APNPayload.SimpleAlertMsg(ALERT));

        //字典模式使用APNPayload.DictionaryAlertMsg
        //payload.setAlertMsg(getDictionaryAlertMsg());

        // 添加多媒体资源
//        payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
//                .setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
//                .setOnlyWifi(true));

        template.setAPNInfo(payload);
        return template;
    }
    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(){
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody("body");
        alertMsg.setActionLocKey("ActionLockey");
        alertMsg.setLocKey("LocKey");
        alertMsg.addLocArg("loc-args");
        alertMsg.setLaunchImage("launch-image");
        // iOS8.2以上版本支持
        alertMsg.setTitle("Title");
        alertMsg.setTitleLocKey("TitleLocKey");
        alertMsg.addTitleLocArg("TitleLocArg");
        return alertMsg;
    }


}